#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
static int global = 0;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void * do_thread(void *arg) {
  printf("do_thread\n");
  int loops = * (int *) arg;
  int i, temp;
  for (i = 0; i < loops; i++) {
    pthread_mutex_lock(&lock);
    temp = gloabl;
    temp++;
    global = temp;
    pthread_mutex_unlock(&lock);
  }
  pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
  int loops;
  if (argc != 2) {
    printf("usage: a.out <#loops>\n");
    exit(EXIT_FAILURE);
  }
  //将argc 转换为整数
  loops = atoi(argv[1]);

  // 创建两个线程
  pthread_t thread[2] = {0};
  int i = 0;
  for (i = 0; i < 2; i++) {
    int ret = pthread_create(&thread[i], NULL, do_thread, NULL);
  }

  pthread_join(thread[0], NULL);
  pthread_join(thread[1], NULL);
  printf("global = %d\n", global);
  return 0;
}
